#
#           _     _                 _
#  ___ _ __(_)___| |__   ___   ___ | |_
# / __| '__| / __| '_ \ / _ \ / _ \| __|
#| (__| |  | \__ \ |_) | (_) | (_) | |_
# \___|_|  |_|___/_.__/ \___/ \___/ \__|
#                automation honor!!!!!!
#
#
# Complete Functional GUI Automated Test
#
# Author: Cristian Cortez
# Date: Thursday, January 5, 2012
# License: WTFPL
# Platform: Windows 64-bits
# Dependencies: Ruby, Gem, Chrome Webdriver
# Gems List: watir, watir-webdriver
#
# Disclaimer:
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

# OS    Expected Location of Chrome
# Linux  /usr/bin/google-chrome
# Mac    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
# Windows XP     %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
# Windows Vista  C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe

puts "Detecting yout Operative System:" + RUBY_PLATFORM


#settings
$host = "http://desarrollo.los2negritos.com/"

$cred = {
    "email" => "demo@demo.com",
    "pwd" => "123456",
    "registerUser" => {
		'nombre' => 'Esteban',
		'apellido' => 'Ripani',
		'email' => 'demo2@demo.com',
		'pwd' => '123456',
		'pwdconfirm' => '123456',
		'telefono' => '155555555',
		'fechaNac' => '1982-12-11',
		'empresa' => 'UAI',
		'ciudad' => 'Rosario',
		'codigoPostal' => '2000',
		'domicilio' => 'Ov. Lagos 940'
     }
}

$browserUTAddTime = {
        "ff" => 0,
        "chrome" => 0,
        "ie" => 3,
        "ieChangeMode" => 8,
        "ie9" => "IE9",
        "ie8" => "IE8",
        "ie7" => "IE7"
}

# Selecting browser
puts "Please select a Browser (ff, ie, chrome):"
$browserSelect = STDIN.gets.split("\n")[0].to_s
# FPO -
# $browserSelect = "chrome"
puts "[info] Starting tests with "+$browserSelect

# Windows
if RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw/ then
        if $browserSelect == 'ie' then
                require 'watir'
                $browser = Watir::Browser.new
                puts "[info] Now you have " + $browserUTAddTime["ieChangeMode"].to_s + " seconds to change the mode"
                sleep $browserUTAddTime[$browserSelect] + $browserUTAddTime["ieChangeMode"]
        else
                require 'watir-webdriver'

                if $browserSelect == 'chrome' then
                        system('start /d "C:\\Users\\%USERNAME%\\AppData\\Local\\Google\\Chrome\\Application\\" chromedriver.exe')
                        $browser = Watir::Browser.new(:remote, :url => "http://localhost:9515")
                else
                        $browser = Watir::Browser.new($browserSelect)
                end
        end
end

# Linux
if RUBY_PLATFORM =~ /linux/ then
        require "rubygems"
        require "watir-webdriver"
        if $browserSelect == 'chrome' then
                $browser = Watir::Browser.new :chrome
        end

        if $browserSelect == 'ff' then
                $browser = Watir::Browser.new :ff
        end
end

# Mac
if RUBY_PLATFORM =~ /darwin/ then
        puts "Configuration for Mac is missing, please modify the script"
end

#
# Test Cases
#
def go webPage
    $browser.goto webPage
end

def testHomePage
    go $host
end

def testLogin
    $browser.goto $host+"cliente/ingreso"
    $browser.text_field(:id, "email").set($cred["email"])
    $browser.text_field(:id, "pwd").set($cred["pwd"])
    $browser.button(:id, "Ingresar").click

        if $browser.link(:text, "Salir").exists? then
                puts "[ok] Login Successful on " + $host
        else
                puts "[fail] Login Failed on " + $host
        end
end

def testLogout
    $browser.link(:text, "Salir").click

        if $browser.link(:text, "Salir").exists? then
                puts "[fail] Logout Failed on " + $host
        else
                puts "[ok] Logout Successful on " + $host
        end
end

def testRegistration
    go $host+'cliente/registrar'
	$browser.text_field(:id, 'nombre').set($cred["registerUser"]["nombre"])
	$browser.text_field(:id, 'apellido').set($cred["registerUser"]["apellido"])
	$browser.text_field(:id, 'email').set($cred["registerUser"]["email"])
	$browser.text_field(:id, 'pwd').set($cred["registerUser"]["pwd"])
	$browser.text_field(:id, 'pwdconfirm').set($cred["registerUser"]["pwdconfirm"])
	$browser.text_field(:id, 'telefono').set($cred["registerUser"]["telefono"])
	$browser.text_field(:id, 'fechaNac').set($cred["registerUser"]["fechaNac"])
	$browser.text_field(:id, 'empresa').set($cred["registerUser"]["empresa"])
	$browser.text_field(:id, 'ciudad').set($cred["registerUser"]["ciudad"])
	$browser.text_field(:id, 'codigoPostal').set($cred["registerUser"]["codigoPostal"])
	$browser.text_field(:id, 'domicilio').set($cred["registerUser"]["domicilio"])


    $browser.button(:id, "Registrar").click
	
	if $browser.p(:text, "Usted se registro correctamente.").exists? then
		puts "[ok] Register Successful on " + $host
	else
		puts "[fail] Register Failed on " + $host
	end
end


def testCompra
	#necesita que el usuario este loggeado como cliente
	if $browser.link(:text, "Salir").exists? then
		#esta loggeado no hacer nada
	else
		testLogin
	end


    go $host
	$browser.link(:text, "Productos").click
	$browser.link(:text, "2").click
	$browser.link(:text, "Agregar").click
	$browser.link(:id, "cart-modal-start").click
	$browser.link(:text, "Confirmar").click
	$browser.link(:id, "disparar-pedido").click
	
	if $browser.link(:text, "Ver detalle de pedido").exists? then
		puts "[ok] Compra Successful on " + $host
	else
		puts "[fail] Compra Failed on " + $host
	end
end

def closeBrowser
    puts "press a key to close the browser"
    STDIN.gets
    $browser.close
end

#Tests to execute
#testHomePage
testLogin
testCompra
testLogout
# testRegistration
